package com.yuandian.business.monitor.controller;



import com.yuandian.api.business_monitor.po.AlarmParams;
import com.yuandian.api.business_monitor.po.AlarmReverseAnalysisParams;
import com.yuandian.api.business_monitor.vo.AlarmBusinessStepVo;
import com.yuandian.api.business_monitor.vo.AlarmIndicatorsVo;
import com.yuandian.api.business_monitor.vo.BaseBusinessAlarmVo;
import com.yuandian.api.business_monitor.vo.BusinessStepVo;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.business.monitor.service.BusiAlarmService;
import com.yuandian.business.monitor.service.IBusiDetailStatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 告警业务查询
 * </p>
 *
 * @author fucx
 * @since 2020-11-10
 */
@RestController
@RequestMapping("/alarm/business")
@Api(value = "business", tags = "基础告警接口")
public class BusiAlarmController {

	@Autowired
	private IBusiDetailStatService busiDetailStatService;

	@Autowired
	private BusiAlarmService busiAlarmService;

	/**
	 * 查询告警业务信息
	 *
	 * @return 通用返回对象
	 */
	@PostMapping("/getBusinessAlarm")
	@ApiOperation("查询告警业务信息")
	public R getBusinessAlarm(@RequestBody AlarmParams params) {
		R result = busiDetailStatService.getBusinessAlarm(params);
		return result;
	}

	/**
	 * 下载
	 *
	 * @return 通用返回对象
	 */
	@PostMapping("/exportExcel")
	@ApiOperation("查询告警业务导出")
	public void exportExcel(@RequestBody AlarmParams params, HttpServletResponse response) {
		try {
			busiDetailStatService.exportExcel(params, response);
		} catch (IOException e) {

		}
	}

	/**
	 * 告警业务查询下钻到查询业务列表信息，后面修改为基础告警
	 *
	 * @return 通用返回对象
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId",  			value = "系统id",  			paramType = "query", 	required = true, 	dataType = "Integer"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "pageNum",   			value = "页码", 				paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", 			value = "每页大小", 			paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "category", 			value = "告警分类(BPM/APM)", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "headerFlag", 			value = "是否首端", 			paramType = "query", 	required = true, 	dataType = "Integer"),
			@ApiImplicitParam(name = "alarmIndicatorIds", 		value = "告警指标", 			paramType = "query", 	required = true, 	dataType = "Array"),
			@ApiImplicitParam(name = "alarmStates", 		value = "告警状态", 			paramType = "query", 	required = true, 	dataType = "Array"),
			@ApiImplicitParam(name = "businessCodes", 		value = "业务编码", 			paramType = "query", 	required = true, 	dataType = "Array"),
			@ApiImplicitParam(name = "segmentIds", 		value = "网端id", 			paramType = "query", 	required = true, 	dataType = "Array"),
			@ApiImplicitParam(name = "stepCodes", 		value = "步骤id", 			paramType = "query", 	required = true, 	dataType = "Array"),
			@ApiImplicitParam(name = "alarmLevelIds", 				value = "告警级别", paramType = "query", 	required = true, 	dataType = "Array")
	})
	@PostMapping("/getBaseAlarmList")
	@ApiOperation("查询基础告警")
	public R getBaseAlarmList(@RequestBody AlarmParams params) {
		return busiDetailStatService.getBusinessList(params);
	}

	/**
	 * 告警反推分析
	 * @param params 告警反推分析请求参数
	 * @return 通用返回对象
	 */
	@PostMapping("/reverseAnalysis")
	@ApiOperation("告警反推分析")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id",  			value = "告警id",  			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "statisticsEndTime", 			value = "统计结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "statisticsStartTime", 			value = "统计开始时间", 			paramType = "query", 	required = true, 	dataType = "String"),
	})
	public R reverseAnalysis(@RequestBody AlarmReverseAnalysisParams params) {
		Map<String, Object> map = null;
		try {
			map = busiDetailStatService.reverseAnalysis(params);
			return R.ok(map);
 		} catch (Exception e) {
			e.printStackTrace();
			return R.failed(-1, "分析异常");
 		}
	}
	/**
	 * 根据id查询告警详情
	 *
	 * @param id
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id",  			value = "告警id",  			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "String"),
	})
	@GetMapping("/getBusinessAlarmDetail")
	@ApiOperation("根据id查询告警详情")
	public R getBusinessAlarmDetail(String id, String startTime, String endTime) {

		BaseBusinessAlarmVo detail = busiDetailStatService.getBusinessAlarmDetail(id, startTime, endTime);

		return R.ok(detail);
	}

	/**
	 * 根据步骤id查询business_detail
	 *
	 * @param id
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@GetMapping("/getBusinessDetailList")
	@ApiOperation(" 根据步骤id查询business_detail")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id",  			value = "告警id",  			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "String"),
	})
	public R getBusinessDetailList(String id, String startTime, String endTime) {
		return busiDetailStatService.getBusinessDetailList(id, startTime, endTime);
	}

	/**
	 * 根据步骤id查询business_step
	 *
	 * @param id
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@GetMapping("/getStepDetailList")
	@ApiOperation("根据步骤id查询business_step")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id",  			value = "告警id",  			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "String"),
	})
	public R getStepDetailList(String id, String startTime, String endTime) {
		List<AlarmBusinessStepVo> list = busiDetailStatService.getStepDetailList(id, startTime, endTime);
		return R.ok(list);
	}

	/**
	 * 按照系统ID、业务编码及时间范围分类统计各类型告警(环形图)
	 * @param params  查询参数
	 * @return 返回通用结果集
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId",  			value = "系统id",  			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "businessCode",  			value = "业务编码",  			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "segmentId",  			value = "网端id",  			paramType = "query", 	required = false, 	dataType = "String"),
			@ApiImplicitParam(name = "alarmState",  			value = "告警状态",  			paramType = "query", 	required = false, 	dataType = "String"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "String"),
	})
	@PostMapping("/getBusinessAlarmPercent")
	@ApiOperation("按照系统ID、业务编码及时间范围分类统计各类型告警(环形图)")
	public R getBusinessAlarmPercent(@RequestBody AlarmParams params) {
		return R.ok(busiAlarmService.getBusinessAlarmPercent(params));
	}

	/**
	 * 按照系统ID、业务编码及时间范围分类统计各等级告警(环形图)
	 * @param params  查询参数
	 * @return 返回通用结果集
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId",  			value = "系统id",  			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "businessCode",  			value = "业务编码",  			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "segmentId",  			value = "网端id",  			paramType = "query", 	required = false, 	dataType = "String"),
			@ApiImplicitParam(name = "alarmState",  			value = "告警状态",  			paramType = "query", 	required = false, 	dataType = "String"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "String"),
	})
	@PostMapping("/getBusinessAlarmLevelPercent")
	@ApiOperation("按照系统ID、业务编码及时间范围分类统计各等级告警(环形图)")
	public R getBusinessAlarmLevelPercent(@RequestBody AlarmParams params) {
		return R.ok(busiAlarmService.getBusinessAlarmLevelPercent(params));
	}

	/**
	 * 按照系统ID、业务编码、时间范围、端口ID查询各告警对应 不同等级 的告警总数列表
	 * @param params  查询参数（系统ID、业务编码、时间范围、端口ID）
	 * @return 返回通用结果集
	 */
	@PostMapping("" +
			"/getBusinessGradeDate")
	@ApiOperation("按照系统ID、业务编码、时间范围、端口ID查询各告警对应 不同等级 的告警总数列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId",  			value = "系统id",  			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "businessCode",  			value = "业务编码",  			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "segmentId",  			value = "网端id",  			paramType = "query", 	required = false, 	dataType = "String"),
			@ApiImplicitParam(name = "alarmState",  			value = "告警状态",  			paramType = "query", 	required = false, 	dataType = "String"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "String"),
	})
	public R getBusinessGradeDate(@RequestBody AlarmParams params) {
		return R.ok(busiAlarmService.getBusinessGradeDate(params));
	}

	/**
	 * 按照系统ID、业务编码、时间范围、端口ID和告警类型id查询各告警详情列表
	 * @param params  查询参数（系统ID、业务编码、时间范围、端口ID）
	 * @return 返回通用结果集
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId",  			value = "系统id",  			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "businessCode",  			value = "业务编码",  			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "businessCodes",  			value = "业务编码",  			paramType = "query", 	required = true, 	dataType = "Array"),
			@ApiImplicitParam(name = "alarmTypeId",  			value = "告警类型",  			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "segmentId",  			value = "网端id",  			paramType = "query", 	required = false, 	dataType = "String"),
			@ApiImplicitParam(name = "alarmState",  			value = "告警状态",  			paramType = "query", 	required = false, 	dataType = "String"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "String"),
	})
	@PostMapping("/getBusinessAlarmList")
	@ApiOperation("按照系统ID、业务编码、时间范围、端口ID和告警类型id查询各告警详情列表")
	public R getBusinessAlarmList(@RequestBody AlarmParams params) {
		return R.ok(busiAlarmService.getBusinessAlarmList(params));
	}


	/**
	 * 查询基础指标分类
	 *
	 * @param params
	 *            参数
	 * @return 通用返回对象
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId",  			value = "系统id",  			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "businessCode",  			value = "业务编码",  			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "businessCodes",  			value = "业务编码",  			paramType = "query", 	required = true, 	dataType = "Array"),
			@ApiImplicitParam(name = "alarmTypeId",  			value = "告警类型",  			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "segmentId",  			value = "网端id",  			paramType = "query", 	required = false, 	dataType = "String"),
			@ApiImplicitParam(name = "alarmState",  			value = "告警状态",  			paramType = "query", 	required = false, 	dataType = "String"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "String"),
	})
	@PostMapping("/queryBaseIndicatorsList")
	@ApiOperation("查询基础指标分类")
	public R queryBaseIndicatorsList(@RequestBody AlarmParams params) {
		return R.ok(busiAlarmService.queryBaseIndicatorsList(params));
	}

	/**
	 * 按照系统ID、业务编码及时间范围分类统计各类型告警(环形图)
	 * @param params  查询参数
	 * @return 返回通用结果集
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId",  			value = "系统id",  			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "businessCode",  			value = "业务编码",  			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "businessCodes",  			value = "业务编码",  			paramType = "query", 	required = true, 	dataType = "Array"),
			@ApiImplicitParam(name = "alarmTypeId",  			value = "告警类型",  			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "segmentId",  			value = "网端id",  			paramType = "query", 	required = false, 	dataType = "String"),
			@ApiImplicitParam(name = "alarmState",  			value = "告警状态",  			paramType = "query", 	required = false, 	dataType = "String"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "String"),
	})
	@PostMapping("/getBusinessMonitorAlarmPercent")
	@ApiOperation("按照系统ID、业务编码及时间范围分类统计各类型告警(环形图)")
	public R getBusinessMonitorAlarmPercent(@RequestBody AlarmParams params) {
		return R.ok(busiAlarmService.getBusinessMonitorAlarmPercent(params));
	}

	/**
	 * 校验表达式
	 *
	 * @param indicator
	 * @return 结果
	 */
	@PostMapping("/checkExpression")
	public R checkExpression(@RequestBody AlarmIndicatorsVo indicator) {
		R result = busiAlarmService.checkExpression(indicator);
		return result;
	}

}
